<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Baocao extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'qlsv_model', 'hocphi');
		$this->load->module_language('member', 'qlsv');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
	}
	
	public function export()
	{
		$from = $this->input->post('expfrom');
		$to = $this->input->post('expto');
		if($from !='')
		{
			$allhocphi = $this->hocphi->get_list_hoc_phi_from_to($from,$to);
			$totalhocphi= $this->hocphi->get_total_hoc_phi_from_to($from,$to);
			
			$excels = array();
			$header = array('STT','Người nộp','Mã học viên','Trạng thái','Ngày thu','Số tiền');
			$excels[1]= $header;
			$excels[2]= array('','Tổng','','','',number_format($totalhocphi));
			$j = 1;
			if(is_array($allhocphi))
			{
				foreach ($allhocphi as $item)
				{
					//var_dump($item);exit();
					$arr = array();
					$arr[] = $j;
					$arr[] = $item['tenhv'];
					$arr[] = $item['mahv'];
					$arr[] = $item['lock']==1?'Đã thu tiền':'Chưa thu tiền';
					$arr[] = date('d/m/Y', strtotime($item['ngaythu']));
					$arr[] = number_format($item['sotiennop']);
					$excels[] = $arr;
					$j++;
				}
			}
			// Load thư viện xuất file
			$this->load->library('excel');
			$this->excel->setAll('UTF-8', false, 'Danh sach hoc sinh');
			$this->excel->addArray($excels);
			$this->excel->generateXML('bao-cao-thu-thien-tu-'.$to.'-'.$from.'-t19');
		
		}
		else {
			redirect('/member/baocao', 'refresh');
		}
	}
	public function custom($from='',$to='')
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Bảng thu học phí';
		$data['description'] = 'Bảng thu học phí';
		$data['duration'] = ' Tùy chỉnh';
		$this->access->restrict();
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->form_validation->set_rules('from','lang:form_cmnd','required');
		if($from =='')
		{
			$from = $this->input->post('from');
		}
			
		if($to =='')
		{
			$to = $this->input->post('to');
		}
		$data['expto'] = $to;
		$data['expfrom']=$from;
		if($this->form_validation->run() == TRUE)
		{
			if($from =='')
			{
				$from = $this->input->post('from');
			}
			
			if($to =='')
			{
				$to = $this->input->post('to');
			}
			$config = array();
			$config['base_url'] = base_url().'member/hocphi/custom/';
			$config['total_rows'] = $this->hocphi->count_all_hocphi_custom($from,$to);
			$config['per_page'] = 30;
			$config['uri_segment'] = 4;
			$config['num_links'] = 5;
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$config['next_link'] = 'Next';
			$config['prev_link'] = 'Prev';
			$this->pagination->initialize($config);
			$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
			
			$data['allhocphi'] = $this->hocphi->get_list_hoc_phi_from_to($from,$to);
			$data['totalhocphi']= $this->hocphi->get_total_hoc_phi_from_to($from,$to);
			$data['links'] = $this->pagination->create_links();
			$this->_template['page']	= 'users/baocao/list';
			$this->system_library->load($this->_template['page'], $data);
		}
		else {
			redirect('/member/baocao', 'refresh');
		}
		
	}
 
	public function index($duration='') 
	{
		//$this->access->restrict('Member.Baocao.Index');
		$data['ItemID'] = 'User';
		$data['title'] = 'Bảng thu học phí';
		$data['description'] = 'Bảng thu học phí';
		
		$this->access->restrict();
		$this->load->library('basic_library');
		$this->load->library('pagination');
		
		$config = array();
		$config['base_url'] = base_url().'member/hocphi/index/';
		$config['total_rows'] = $this->hocphi->count_all_hocphi($duration);
		$config['per_page'] = 30;
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Prev';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		
		$data['expto'] = '';
		$data['expfrom']='';
		$data['allhocphi'] = $this->hocphi->get_list_hoc_phi_duration($duration);
		$data['totalhocphi']= $this->hocphi->get_total_hoc_phi_duration($duration);
		$data['links'] = $this->pagination->create_links();		
		switch ($duration)
		{
			case 'yesterday': $data['duration'] = ' hôm qua'; break;
			case 'thismonth': $data['duration'] = ' tháng này'; break;
			case 'thisyear': $data['duration'] = ' năm nay'; break;
			case 'quater': $data['duration'] = ' quý này'; break;
			case 'quater': $data['duration'] = ' quý này'; break;
			default: $data['duration'] = ' hôm nay';
		}
		$this->_template['page']	= 'users/baocao/list';
		$this->system_library->load($this->_template['page'], $data);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/products.php */